package com.zhiche.lisa.lspm.controller;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.driver.Driver;
import com.zhiche.lisa.lspm.dao.model.lsp.LspInfo;
import com.zhiche.lisa.lspm.dto.DriverCheckinDTO;
import com.zhiche.lisa.lspm.dto.DriverDTO;
import com.zhiche.lisa.lspm.dto.LspInfoDTO;
import com.zhiche.lisa.lspm.dto.VehicleDTO;
import com.zhiche.lisa.lspm.service.carrier.ICarrierInfoService;
import com.zhiche.lisa.lspm.service.driver.IDriverCheckinService;
import com.zhiche.lisa.lspm.service.driver.IDriverService;
import com.zhiche.lisa.lspm.service.lsp.LspInfoService;
import com.zhiche.lisa.lspm.service.utils.IntegrationUtil;
import com.zhiche.lisa.lspm.service.utils.MdmUtil;
import com.zhiche.lisa.lspm.vo.common.TransportationTypeVO;
import com.zhiche.lisa.lspm.vo.vehicle.WeChatVehicle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Created by zhaoguixin on 2018/7/23.
 */
@RestController
@RequestMapping(value = "/export")
public class ExportController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExportController.class);

    @Autowired
    private LspInfoService lspInfoService;

    @Autowired
    private IntegrationUtil integrationUtil;

    @Autowired
    private ICarrierInfoService carrierInfoService;

    @Autowired
    private IDriverService driverService;

    @Autowired
    private IDriverCheckinService driverCheckinService;

    @Autowired
    private MdmUtil mdmUtil;

    /**
     * 导出承运商
     *
     * @param lspId
     * @return
     */
    @PostMapping(value = "/lspInfo")
    public RestfulResponse<String> lspInfo(Long lspId) {
        LOGGER.info("LspInfoController.lspInfoLine（获取承运商基础信息列表） params : {} ", lspId);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            LspInfo lspInfo = lspInfoService.selectById(lspId);
            LspInfoDTO lspInfoDTO = new LspInfoDTO();
            BeanUtils.copyProperties(lspInfo, lspInfoDTO);

            // 服务类型
            if (!StringUtils.isEmpty(lspInfoDTO.getServiceType())) {
                String serviceType = mdmUtil.getServiceTypeByCodes(lspInfo.getServiceType());
                if (serviceType.contains("板车运输")) {
                    serviceType = serviceType.replaceAll("板车运输", "公路");
                }
                if (serviceType.contains("铁路运输")) {
                    serviceType = serviceType.replaceAll("铁路运输", "铁运");
                }
                if (serviceType.contains("人送地跑")) {
                    serviceType = serviceType.replaceAll("人送地跑", "人送");
                }
                if (serviceType.contains("船运")) {
                    serviceType = serviceType.replaceAll("船运", "水运");
                }
                lspInfoDTO.setServiceType(serviceType.replaceAll(";", ","));
            } else {
                lspInfoDTO.setServiceType("公路");
            }

            String requstId = integrationUtil.exportLspToOTM(lspInfoDTO);
            result.setData(requstId);

        } catch (BaseException e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 导出车辆
     *
     * @param carrierId 牵引车ID
     */
    @PostMapping(value = "/vehicle")
    public RestfulResponse<String> vehicle(Long carrierId) {
        LOGGER.info("LspInfoController.lspInfoLine（获取承运商基础信息列表） params : {} ", carrierId);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            WeChatVehicle vehicle = carrierInfoService.getWeChatVehicleById(carrierId);
            VehicleDTO vehicleDTO = new VehicleDTO();
            BeanUtils.copyProperties(vehicle, vehicleDTO);
            TransportationTypeVO transportationType = null;
            transportationType = mdmUtil.getTransportTypeById(vehicleDTO.getTrailerTypeId());
            if (Objects.nonNull(transportationType)
                    && !StringUtils.isEmpty(transportationType.getValue())) {
                if (transportationType.getValue().equals("三位平板")) {
                    vehicleDTO.setTrailerTypeName("平板三位板");
                }
                if (transportationType.getValue().equals("四位平板")) {
                    vehicleDTO.setTrailerTypeName("平板四位板");
                }
                if (transportationType.getValue().equals("六位平板")) {
                    vehicleDTO.setTrailerTypeName("六位车");
                }
                if (transportationType.getValue().equals("单车")) {
                    vehicleDTO.setTrailerTypeName("单车");
                }
                if (transportationType.getValue().equals("中置轴")) {
                    vehicleDTO.setTrailerTypeName("中置轴");
                }
                if (transportationType.getValue().equals("五位平板")) {
                    vehicleDTO.setTrailerTypeName("平板五位板");
                }
                if (transportationType.getValue().equals("七位平板")) {
                    vehicleDTO.setTrailerTypeName("七位车");
                }
            }
            integrationUtil.exportVehicleToOTM(vehicleDTO);
        } catch (BaseException e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 导出车辆
     *
     * @param driverId 司机ID
     * @return
     */
    @PostMapping(value = "/driver")
    public RestfulResponse<String> driver(Long driverId) {
        LOGGER.info("LspInfoController.lspInfoLine（获取承运商基础信息列表） params : {} ", driverId);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            Driver driver = driverService.selectById(driverId);
            DriverDTO driverDTO = new DriverDTO();
            BeanUtils.copyProperties(driver, driverDTO);
            integrationUtil.exportDriverToOTM(driverDTO);
        } catch (BaseException e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 导出司机报班
     *
     * @param checkinId 司机ID
     * @return
     */
    @PostMapping(value = "/driverCheckin")
    public RestfulResponse<String> driverCheckin(Long checkinId) {
        LOGGER.info("LspInfoController.driverCheckin（导出司机报班） params : {} ", checkinId);
        RestfulResponse result = null;
        try {
            DriverCheckinDTO driverCheckinDTO = driverCheckinService.getDriverCheckinById(checkinId);
            if (Objects.isNull(driverCheckinDTO) && !StringUtils.isEmpty(driverCheckinDTO.getTrailerTypeId())) {
                String value = mdmUtil.getTransportTypeById(new Long(driverCheckinDTO.getTrailerTypeId())).getValue();
                driverCheckinDTO.setTrailerTypeName(value);
            }
            result = integrationUtil.exportCheckin(driverCheckinDTO);
        } catch (BaseException e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LspInfoController.lspInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}
